package com.auto.learning.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.ReplyBean;
import com.auto.learning.event.Handle;
import com.auto.learning.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

@SuppressLint("NewApi")
public class BookCommentFragmentAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<ReplyBean> list;
	private Handle handle;
	DisplayImageOptions options;
	
	public BookCommentFragmentAdapter(Context context,ArrayList<ReplyBean> list,Handle handle){
		this.context = context;
		this.list = list;
		this.handle = handle;
		initImageLoader();
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getReplyId();
	}

	ViewHolder holder = null;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ReplyBean bean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.comment_adapter3, null);
			holder = new ViewHolder();
			holder.fg_me_userpic = (CircleImageView) convertView.findViewById(R.id.fg_me_userpic);
			holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_reusername = (TextView) convertView.findViewById(R.id.tv_reusername);
			holder.item_main_view = (LinearLayout) convertView.findViewById(R.id.item_main_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(bean.getIsSelf() == 2){
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.main_green_color));
		}else {
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.text_comment_username_color));
		}
		ImageLoader.getInstance().displayImage(bean.getFaceUrl(), holder.fg_me_userpic,options);
		if (TextUtils.isEmpty(bean.getReplyTo())) {
			holder.tv_comment.setText(bean.getReply());
		}else {
			holder.tv_comment.setText("回复"+ bean.getReplyTo() + ": " + bean.getReply());
		}
		holder.tv_time.setText(bean.getCreateTime());
		holder.tv_username.setText(bean.getUserName());
		return convertView;
	}
	
	class ViewHolder{
		CircleImageView fg_me_userpic;
		TextView tv_username,tv_comment,tv_time,tv_reusername;
		LinearLayout item_main_view;
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_user)
		.showImageForEmptyUri(R.drawable.me_user)
		.showImageOnFail(R.drawable.me_user).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}
}
